package info.techgeek.labs.regex;

import info.techgeek.labs.String.StringSupplier;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class QueryParamFetcher {

	public static void main(String args[]) throws IOException{
		
		StringSupplier stringSupplier = new StringSupplier();
		String string  = stringSupplier.getString();
		System.out.println("String length "+string.length());
		
		long t = System.currentTimeMillis(); 
		QueryParamFetcher fetcher = new QueryParamFetcher();
		
		List<String> list = (ArrayList<String>)fetcher.getQueryParams(string); 
		System.out.println("--------------------------------------------------");
		System.out.println("Time Taken For Split Logic :" +( System.currentTimeMillis() -t));
	}
	
	public List<String> getQueryParams(String query)
	{
		Pattern p = Pattern.compile("&[a-z0-9_\\-A-Z]+");
		Matcher m = p.matcher(query);
		List<String> aList = new ArrayList<String>(); 
		String str ;
		while (m.find())
		{
			str = m.group();
//			System.out.println(str);
			str = str.substring(1);
			if(!aList.contains(str))
				aList.add(str);
//			System.out.println(str);
		}
		return aList;
	}

	public List<String> getParsedParams(String openString){
		
	 	System.out.println("Begin getParsedParams "+openString);
		String suffix = "";
		List<String> list = new ArrayList<String>(); 
		boolean startAppend = false;
		for(int i = 0;i<openString.length();i++){
			char character =  openString.charAt(i);
			if(startAppend){
				if(",".equals(String.valueOf(character)) || " ".equals(String.valueOf(character))  || "|".equals(String.valueOf(character)) || "'".equals(String.valueOf(character))){
//					List<IiParam> paramList = (ArrayList<IiParam>)navigationDao.getParamType(suffix);
					list.add(suffix);
					suffix = "";
					startAppend = false;
				}else{
					suffix += character;
				}
			}
			if("&".equals(String.valueOf(character))){
				if(suffix.equals(String.valueOf(character))){
					suffix = "";
					startAppend = false;
				}else{
					startAppend = true;
				}
			}
		}
		if(!suffix.equals("")){
			list.add(suffix);
		}
		System.out.println("End getParsedParams "+list.size());
		return list;
	}

}
